﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YFTOpenAPI.Model;

namespace YY.Web.MyAgent.financial_information
{
    public partial class WithdrawCash : BaseFunction.LoginHelp
    {
        private string ip = ConfigurationManager.AppSettings["ip"];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                double Money = 0;
                if (IsFund(out Money))
                {
                    HiddenMoney.Value = Money+"";
                    lbl_Money.Text = Money+"";
                }
                else
                {
                    Response.Redirect("/Center.aspx");
                }
            }
        }

        private bool IsFund(out double f)
        {
            double funds=0;
            YFTOpenAPI.Model.YiyangFunds yfModel = new YiyangFunds();
            string UserInfoID = base.GetSessionModel().amount;//银付通用户编号
            YFTOpenAPI.TransactionAPI openapi = new YFTOpenAPI.TransactionAPI("yiyang", ip, "27CAC2A3-F0C1-449C-8D59-62A9A263E48C");
            YFTOpenAPI.Model.ResponseData responseData = openapi.GetFunds(UserInfoID);
            if (responseData.statuscode == 1)
            {
                //查询成功
                List<YFTOpenAPI.Model.YiyangFunds> fundsinforlist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<YFTOpenAPI.Model.YiyangFunds>>(responseData.data.ToString());

                funds = Convert.ToDouble(fundsinforlist[0].DisplayCashWallet);//现金钱包 钱包余额
               
            }
            else
            {
                //错误信息
                string message = responseData.message;
            }
            if (funds > 0)
            {
                f=funds;
                return true;
            }
            else
            {
                f = funds;
                return false;
            }
        }

        public static string Decode(string str, string key)
        {

            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            byte[] buffer = new byte[str.Length / 2];

            for (int i = 0; i < (str.Length / 2); i++)
            {

                int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);

                buffer[i] = (byte)num2;

            }

            MemoryStream stream = new MemoryStream();

            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);

            stream2.Write(buffer, 0, buffer.Length);

            stream2.FlushFinalBlock();

            stream.Close();

            return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());

        }
    }
}